<!DOCTYPE html>
<html lang="zh-cn">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="author" content="Zhou Wei <zromyk@163.com>">
  <title>Raspberry Pi 3B+ Web server</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" href="/style/html/pure.css">
  <link rel="stylesheet" href="/style/html/main.css">
  <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
  <!-- <link rel="stylesheet" href="https://apps.bdimg.com/libs/highlight.js/9.1.0/styles/default.min.css"> -->
<link rel="stylesheet" href="/style/article/highlight/default.min.css">
<link rel="stylesheet" href="/style/article/pell-1.0.6/dist/pell.css">

</head>
<body>
  <div id="menu-background"></div>
  <div id="menu">
    <div class="pure-menu pure-menu-horizontal">
  <ul class="pure-menu-list block-middle">
    <li class="pure-menu-item">
  <a class="pure-menu-heading" href="/index.html">ZROMYK</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/index.html">主页</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/archive/index.html">归档</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/download/index.html">下载</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/feedback/index.html">反馈</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/about/index.html">关于我</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="https://github.com/zromyk"><i class="fa fa-github" style="font-size:32px"></i></a>
</li>

  </ul>
</div>

  </div>
  <div id="layout">
    <div class="content">
      <div id="nav">
    <div id="navigation" class="navigation">
  <ul class="pure-menu-list">
    <li class="pure-menu-item">
  <a class="pure-menu-link nav3" onclick="animateByNav()" href="#nginx-php7">一、安装 Nginx 和 PHP7</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#1-pi-nginx">1.在 Pi 的终端运行以下命令安装Nginx</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#2-pi-php7">2.在 Pi 的终端运行以下命令安装PHP7</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#3-nginx-nginx-php">3.接下来配置 Nginx 来让 Nginx 能处理 PHP</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav5" onclick="animateByNav()" href="#1">1. 进入编辑</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav5" onclick="animateByNav()" href="#2">2. 将其中的如下内容</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav5" onclick="animateByNav()" href="#3">3. 修改一下上传文件大小限制：</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav5" onclick="animateByNav()" href="#4-nginx">4. 最后重启 Nginx</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav5" onclick="animateByNav()" href="#5-php">5. 添加一个PHP测试文件</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav5" onclick="animateByNav()" href="#6">6. 当然你也可以放一个图形界面，监控树莓派运行状态(可选)</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav3" onclick="animateByNav()" href="#mysql">二、编译安装mysql</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#1-mysql-server">1. 安装 mysql server</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#2-root">2. 安装完毕以后，root密码默认为空。即任意密码都可以登录。</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#3-root">3. 设置root密码</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#4-mysql">4. ~~操作Mysql~~</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#5">5. ~~开启远程访问，允许远程登录~~</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#6_1">6. ~~设置账号权限~~</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav3" onclick="animateByNav()" href="#mysql_1">三、彻底卸载MySQL的方法</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#1-mysql">1. 删除mysql</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#2_1">2. 清理残留数据</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#3-mysql">3. 重新安装mysql</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav3" onclick="animateByNav()" href="#phpmyadmin">四、安装phpMyAdmin</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#1_1">1. 下载</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#2-web">2. 移动到WEB目录下</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#3_1">3. 复制一个配置文件</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#4-host-localhost-127001">4. 并将host的 <span style="overflow-x: auto; max-width:100%; display:inline;"><code>localhost</code></span> 修改为<span style="overflow-x: auto; max-width:100%; display:inline;"><code>127.0.0.1</code></span></a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link nav4" onclick="animateByNav()" href="#5-php70-mbstring">5. 安装php7.0-mbstring</a>
</li>

  </ul>
</div>

</div>
<div id="content-articles">
  <h1 id="Raspberry Pi 3B+ Web server" class="content-subhead">Raspberry Pi 3B+ Web server</h1>
  <p>
    <span>2020-03-08</span>
    <span><span class="post-category post-category-raspberry-pi">Raspberry Pi</span></span>
    <button id="button-markdownEditor" class="pure-button" onclick="markdownEditor()">启用编辑</button>
    <button id="button-save" class="pure-button" onclick="save()">保存</button>
  </p>
  <div id="content-articles-markdownEditor" style="display: none;">
    <h1>编辑 power by pell</h1>
    <div id="editor" class="pell"></div>
    <div style="margin-top:20px;">
        <h3>Text output:</h3>
        <div id="text-output"></div>
    </div>
    <div style="margin-top:20px;">
        <h3>HTML output:</h3>
        <pre id="html-output"></pre>
    </div>
  </div>
  <div id="content-articles-markdown">
    <h3 id="nginx-php7">一、安装 Nginx 和 PHP7</h3>
<blockquote class="content-quote">
<p>PHP7.0 可能已无法使用，请安装最新版的 PHP</p>
</blockquote>
<h4 id="1-pi-nginx">1.在 Pi 的终端运行以下命令安装Nginx</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo apt install -y nginx
sudo systemctl restart nginx
</code></span></code></pre>
<h4 id="2-pi-php7">2.在 Pi 的终端运行以下命令安装PHP7</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo apt install -y php7.0-fpm php7.0-cli php7.0-curl php7.0-gd php7.0-mcrypt php7.0-cgi php7.0-mysql
sudo systemctl restart php7.0-fpm
</code></span></code></pre>
<blockquote class="content-quote">
<p>如果安装成功，可通过 http://树莓派IP 访问到 Nginx 的默认页。<br />
Nginx 的根目录在 /var/www/html。</p>
</blockquote>
<h4 id="3-nginx-nginx-php">3.接下来配置 Nginx 来让 Nginx 能处理 PHP</h4>
<h5 id="1">1. 进入编辑</h5>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo vim /etc/nginx/sites-available/default
</code></span></code></pre>
<h5 id="2">2. 将其中的如下内容</h5>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell"># Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
}
</code></span></code></pre>
<p>替换为</p>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
}
location ~\.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
client_max_body_size 256m;
</code></span></code></pre>
<h5 id="3">3. 修改一下上传文件大小限制：</h5>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell"># 打开配置文件
sudo vim /etc/php/7.0/fpm/php.ini
# 每个脚本运行的最长时间，单位秒,0为无限
max_execution_time = 0

# 每个脚本可以消耗的时间，单位也是秒
max_input_time = 300
# 脚本运行最大消耗的内存
memory_limit = 256M
# 表单提交最大数据为 8M,针对整个表单的提交数据进行限制的
post_max_size = 20M
# 上载文件的最大许可大小
upload_max_filesize = 10M
</code></span></code></pre>
<h5 id="4-nginx">4. 最后重启 Nginx</h5>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo service nginx restart
</code></span></code></pre>
<h5 id="5-php">5. 添加一个PHP测试文件</h5>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo vim /var/www/html/index.php
</code></span></code></pre>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code># 添加如下内容：
&lt;?php
phpinfo();
?&gt;
</code></span></code></pre>
<blockquote class="content-quote">
<p>访问 http://192.168.1.120/index.php 结果如下：</p>
</blockquote>
<p><img class="pure-img" alt="20180514185146702" src="https://zromyk.gitee.io/myblog-figurebed/post/Raspberry pie 3B+ Web server.assets/20180514185146702.png" /></p>
<h5 id="6">6. 当然你也可以放一个图形界面，监控树莓派运行状态(可选)</h5>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo apt install -y git
cd /var/www/html
sudo git clone https://github.com/spoonysonny/pi-dashboard.git
sudo mv pi-dashboard status
</code></span></code></pre>
<blockquote class="content-quote">
<p>用浏览器访问 http://192.168.1.120/status 可看到如下内容：</p>
</blockquote>
<p><img class="pure-img" alt="20180513152714177" src="https://zromyk.gitee.io/myblog-figurebed/post/Raspberry pie 3B+ Web server.assets/20180513152714177.png" /></p>
<h3 id="mysql">二、编译安装mysql</h3>
<blockquote class="content-quote">
<p>MySQL是一个关系型数据库管理系统，如果不需要数据库可以不安装。</p>
</blockquote>
<h4 id="1-mysql-server">1. 安装 mysql server</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">$ sudo apt-get update
$ sudo apt-get install mysql-server
</code></span></code></pre>
<h4 id="2-root">2. 安装完毕以后，root密码默认为空。即任意密码都可以登录。</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">$ sudo mysql -u root
</code></span></code></pre>
<p>回车登录数据库，出现下面提示，表示成功</p>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.1.37-MariaDB-0+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]&gt; 
</code></span></code></pre>
<h4 id="3-root">3. 设置root密码</h4>
<blockquote class="content-quote">
<p><a href="/public/article/Linux--MySQL：ERROR 1698 28000.html">遇到过的问题（点击此处跳转）</a></p>
</blockquote>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">MariaDB [(none)]&gt; use mysql;
MariaDB [mysql]&gt; update mysql.user set authentication_string=PASSWORD('new password '),plugin='mysql_native_password' where user='root';
MariaDB [mysql]&gt; flush privileges;
MariaDB [mysql]&gt; exit;
</code></span></code></pre>
<h4 id="4-mysql">4. ~~操作Mysql~~</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">$ sudo /etc/init.d/mysql restart
# mysql的其他操作    status、start、stop、restart
</code></span></code></pre>
<h4 id="5">5. ~~开启远程访问，允许远程登录~~</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">$ sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf
# 将bind-address这行注释掉
# 或者将127.0.0.1 这个值改为  0.0.0.0
# 然后重启
$ sudo /etc/init.d/mysql restart
</code></span></code></pre>
<h4 id="6_1">6. ~~设置账号权限~~</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">$ mysql -u root -p
$ 输入密码
MariaDB [(none)]&gt; use mysql;
MariaDB [mysql]&gt; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root的密码' WITH GRANT OPTION;
MariaDB [mysql]&gt; flush privileges;
</code></span></code></pre>
<p>然后就可以使用客户端进行连接了！</p>
<h3 id="mysql_1">三、彻底卸载MySQL的方法</h3>
<h4 id="1-mysql">1. 删除mysql</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo apt-get autoremove --purge mysql-server-5.5
udo apt-get remove mysql-common
</code></span></code></pre>
<h4 id="2_1">2. 清理残留数据</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P 
</code></span></code></pre>
<h4 id="3-mysql">3. 重新安装mysql</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install php7-mysql
</code></span></code></pre>
<h3 id="phpmyadmin">四、安装phpMyAdmin</h3>
<p>用过这个的，应该都会安装一下，这是个mysql图形化管理工具，要求在PHP环境下。</p>
<h4 id="1_1">1. 下载</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">wget https://files.phpmyadmin.net/phpMyAdmin/4.8.0.1/phpMyAdmin-4.8.0.1-all-languages.tar.gz
tar zxvf phpMyAdmin-4.8.0.1-all-languages.tar.gz
</code></span></code></pre>
<h4 id="2-web">2. 移动到WEB目录下</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo mv phpMyAdmin-4.8.0.1-all-languages /var/www/html/mysql
</code></span></code></pre>
<h4 id="3_1">3. 复制一个配置文件</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo cp config.sample.inc.php config.inc.php 
</code></span></code></pre>
<h4 id="4-host-localhost-127001">4. 并将host的 <span style="overflow-x: auto; max-width:100%; display:inline;"><code>localhost</code></span> 修改为<span style="overflow-x: auto; max-width:100%; display:inline;"><code>127.0.0.1</code></span></h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo vim config.inc.php
</code></span></code></pre>
<h4 id="5-php70-mbstring">5. 安装php7.0-mbstring</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-shell">sudo apt install -y php7.0-mbstring
</code></span></code></pre>
<blockquote class="content-quote">
<p>浏览器打开：http://192.168.1.120/mysql/<br />
输入相应帐号密码。即可进入！！</p>
</blockquote>
<p><img class="pure-img" alt="20180514185638100" src="https://zromyk.gitee.io/myblog-figurebed/post/Raspberry pie 3B+ Web server.assets/20180514185638100.png" /></p>
  </div>
</div>
 
    </div>
  </div>
  <div id="footer-background">
    <div id="footer">
      <div class="legal pure-g">
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-license"><a href="https://beian.miit.gov.cn/#/Integrated/index">浙ICP备2020038748号</a></p>
  </div>
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-links"><a href="https://github.com/zromyk">GitHub</a></p>
    <p class="legal-copyright">Copyright © 2021 Wei Zhou. 保留所有权利。</p>
  </div>
</div>
    </div>
  </div>
  <!-- <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> -->
  <script src="/style/html/jquery.min.js"></script>
  <script src='/style/article/latex/latest.js?config=TeX-MML-AM_CHTML'></script>
<!-- <script src="https://cdn.geogebra.org/apps/deployggb.js"></script> -->
<script src="/style/article/deployggb.js"></script>
<!-- <script src="https://apps.bdimg.com/libs/highlight.js/9.1.0/highlight.min.js"></script> -->
<script type="text/javascript">
  // 脚本：navigation 随鼠标移动自动变换宽度
  var element = document.getElementById("navigation"); // 获取要操作的元素
  var elementWidth = parseInt(getComputedStyle(element).width);
  var elementLeft = 0;
  var elementRight = 0;
  element.addEventListener('mouseenter', function (event) { // 添加鼠标按下事件的监听器
    elementLeft = element.getBoundingClientRect().left - 10;
    elementRight = element.getBoundingClientRect().left + elementWidth * 3;
    window.addEventListener('mousemove', resize); // 添加全局的鼠标移动事件的监听器
  });

  function resize(event) {
    var minWidth = elementWidth;
    var maxWidth = elementWidth * 2.5;
    // console.log(elementLeft, event.clientX, elementRight, event.clientX - elementLeft + elementWidth / 2);
    if (elementLeft <= event.clientX && event.clientX <= elementRight) {
      var width = event.clientX - elementLeft + elementWidth / 2;
      width = Math.min(width, maxWidth);
      width = Math.max(width, minWidth);
      element.style.width = width + 'px'; // 设置新的宽度样式属性
    }
    else {
      element.style.width = elementWidth + 'px'; // 设置新的宽度样式属性
      stopResize();
    }
  }

  function stopResize() {
    element.style.width = elementWidth + 'px'; // 设置新的宽度样式属性
    // console.log("stopResize", elementLeft, event.clientX, elementRight, event.clientX - elementLeft + elementWidth / 2);
    window.removeEventListener('mousemove', resize); // 移除鼠标移动事件的监听器
  }
</script>
<script src="/style/article/highlight/highlight.min.js"></script>
<script type="text/javascript">
  // 脚本：code语法高亮
  hljs.initHighlightingOnLoad();
</script>
<script>
  function animateByNav() {
    $("html").animate({
        scrollTop: ($(event.target.hash).offset().top - 52)
    }, 300);
  };
</script>
<script src="/style/article/pell-1.0.6/dist/pell.js"></script>
<script>
  // 脚本：自由编辑页面
  var editor = window.pell.init({
    element: document.getElementById('editor'),
    defaultParagraphSeparator: 'p',
    onChange: function(html) {
        document.getElementById('text-output').innerHTML = html
        document.getElementById('html-output').textContent = html
    }
  });

  function markdownEditor() {
    var articles = document.getElementById('content-articles-markdown');
    if (articles.getAttribute("contenteditable") == "true") {
        articles.setAttribute("contenteditable", "false");
        document.getElementById("content-articles-markdownEditor").style.display = "none"; //隐藏
        document.getElementById("button-markdownEditor").innerHTML = "启用编辑";
    } else {
        articles.setAttribute("contenteditable", "true");
        document.getElementById("content-articles-markdownEditor").style.display = ""; //显示
        document.getElementById("button-markdownEditor").innerHTML = "关闭编辑";
    }
  };

  function save() {
      window.alert("保存成功");
  };
</script>

</body>
</html>
